# -*- coding: utf-8 -*-

# Fetch More Example 
# Ported to PyQt4 by Darryl Wallace, 2009 - wallacdj@... 

import sys 
from PySide import QtGui, QtCore 

class FileListModel(QtCore.QAbstractListModel): 
    
    numberPopulated = QtCore.Signal(int)
    
    def __init__(self, parent=None): 
        QtCore.QAbstractListModel.__init__(self, parent) 
        self.fileCount=0     
        self.fileList=[]    #initialize the file list as a Python list. 
    #__init__ 
    
    def rowCount(self, parent): 
        """ 
        parent=QModelIndex 
        """ 
        return self.fileCount 
    #rowCount 
    
    def data(self, index, role=QtCore.Qt.DisplayRole): 
        """ 
        index=QModelIndex 
        """ 
        if not index.isValid(): 
            return None
        
        if index.row()>=len(self.fileList) or index.row()<0: 
            return None 
        
        if role==QtCore.Qt.DisplayRole: 
            return self.fileList[index.row()] 
        elif role==QtCore.Qt.BackgroundRole: 
            batch=(index.row()/100)%2 
            if batch==0: 
                return QtGui.qApp.palette().base()
            else: 
                return QtGui.qApp.palette().alternateBase()
        return None
    #data 
    
    def canFetchMore(self, index): 
        """ 
        index=QModelIndex 
        """ 
        if self.fileCount<len(self.fileList): 
            return True 
        else: 
            return False 
    #canFetchMore 
    
    def fetchMore(self, index): 
        """ 
        Index=QModelIndex 
        """ 
        
        remainder=len(self.fileList)-self.fileCount 
        itemsToFetch=min(100, remainder) 
        
        self.beginInsertRows(QtCore.QModelIndex(), self.fileCount, self.fileCount+itemsToFetch) 
        
        self.fileCount+=itemsToFetch 
        
        self.endInsertRows() 
        
        # self.emit(QtCore.SIGNAL("numberPopulated"), itemsToFetch) 
        
        self.numberPopulated.emit(itemsToFetch)
    #fetchMore 
    
    def setDirPath(self, path): 
        dir=QtCore.QDir(path) 
        
        self.fileList=list(dir.entryList()) # force Python list. 
        self.fileCount=0 
        self.reset() 
    #setDirPath 
#FileListModel 

class Window(QtGui.QWidget): 
    
    def __init__(self, parent=None): 
        QtGui.QWidget.__init__(self, parent) 
        
        self.model = FileListModel(self) 
        self.model.setDirPath(QtCore.QLibraryInfo.location(QtCore.QLibraryInfo.PrefixPath)) 
        
        label = QtGui.QLabel(self.tr("Directory")) 
        lineEdit=QtGui.QLineEdit() 
        label.setBuddy(lineEdit) 
        
        view=QtGui.QListView() 
        view.setModel(self.model) 
        
        
        
        self.logViewer=QtGui.QTextBrowser() 
        self.logViewer.setSizePolicy(QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Preferred)) 
        
        self.connect(lineEdit, QtCore.SIGNAL("textChanged(const QString &)"), self.model.setDirPath) 
        self.connect(lineEdit, QtCore.SIGNAL("textChanged(const QString &)"), 
                     self.logViewer, QtCore.SLOT("clear()")) 
        # self.connect(self.model, QtCore.SIGNAL("numberPopulated"), ) 
        
        self.model.numberPopulated.connect(self.updateLog)
        
        layout=QtGui.QGridLayout() 
        layout.addWidget(label, 0, 0) 
        layout.addWidget(lineEdit, 0, 1) 
        layout.addWidget(view, 1, 0, 1, 2) 
        layout.addWidget(self.logViewer, 2, 0, 1, 2) 
        
        self.setLayout(layout) 
        
        self.setWindowTitle(self.tr("Fetch More Example")) 
    #__init__ 
    
    def updateLog(self, number): 
        #self.logViewer.append(self.tr("%1 items added.").arg(number)) 
        print "%d items added." % (number)
    #updateLog 
#Window 

if __name__=='__main__': 
    qApp=QtGui.QApplication(sys.argv) 
    
    fetchMoreWindow=Window() 
    fetchMoreWindow.show() 
    sys.exit(qApp.exec_())
